package com.xxxx.seckill.service_impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.seckill.mapper.SeckillOrderMapper;
import com.xxxx.seckill.pojo.SeckillOrder;
import com.xxxx.seckill.pojo.User;
import com.xxxx.seckill.service.ISeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author macaber
 * @since 2021-09-30
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {
    @Autowired
        SeckillOrderMapper seckillOrderMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public Long getResult(User user, Long goodsId) {
        SeckillOrder seckillOrder = seckillOrderMapper.selectOne (new QueryWrapper<SeckillOrder> ( ).eq ("user_id", user.getId ( ))
                .eq ("goods_id", goodsId));
           if (seckillOrder!=null){
               return seckillOrder.getOrderId ();
           }else if (redisTemplate.hasKey ("isStockEmpty:"+goodsId)){
               return -1L;
           }else {
               return 0L;
           }

    }
}
